#include"SceneManager.h"

SceneManager::SceneManager(QRect &r)
{
    items = 4;
    for(int i=0; i < items; ++i)
    {
        GameScens.push_back(new MenyScene(i,r));
    }
    activ=0;
}
SceneManager::~SceneManager()
{
    for(int t = 0; t<items; ++t)
    {
        GameScens[t]->~MenyScene();
    }
}

MenyScene* SceneManager:: GetScene(int number)
{
    return GameScens[number];
}

MenyScene* SceneManager::GetCurrentScene()
{
    return GetScene(activ);
}

void SceneManager::ConnectMenis()
{
    //for(int m=0;m<items;m++)
    MenyScene* s = GameScens[0];
    QPushButton* b = s->GetButtom(0);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetServerMeny()));
    b = s->GetButtom(1);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetClientMeny()));

    b = s->GetButtom(3);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotExitButton()));

    //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    s = GameScens[1];
    b = s->GetButtom(0);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetMainMeny()));
    b = s->GetButtom(1);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetGame()));

    s = GameScens[2];
    b = s->GetButtom(0);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetMainMeny()));
    //b = s->GetButtom(1);
    //QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotSetClientGame()));
    b = s->GetButtom(2);
    QObject::connect(b,SIGNAL(clicked()),this,SLOT(slotConnectToTheServer()));

    //for Game Scen
    QObject::connect(GameScens[3],SIGNAL(signalPresEscKey()),this,SLOT(slotPresEscKey()));
    QObject::connect(GameScens[3],SIGNAL(signalPresKey(QKeyEvent*)),this,SLOT(slotPresKey(QKeyEvent*)) );
}

void SceneManager::slotSetServerMeny()
{
    activ=1;
    emit signalChanged(GetScene(activ));
    QString nick = GameScens[0]->GetEdit(0)->text();
    qDebug()<<nick;
    emit signalCreateServer(nick);

}

void SceneManager::slotSetClientMeny()
{
    activ=2;
    emit signalChanged(GetScene(activ));
}

void SceneManager::slotExitButton()
{
    emit signalEnd();
}

void SceneManager::slotSetMainMeny()
{
    activ=0;
    emit signalChanged(GetScene(activ));
}

void SceneManager::slotSetGame()
{
    activ=3;
    emit signalChanged(GetScene(activ));
    emit signalGameStart();
}

void SceneManager::slotConnectToTheServer()
{
    if((GameScens[2]->GetEdit(0)->text().length()>0) &&
            (GameScens[2]->GetEdit(1)->text().length()>0) &&
            (GameScens[2]->GetEdit(2)->text().length()>0))
    {
      // emit signalChanged(GetScene(activ));
       emit signalClientGameConnect(GameScens[2]->GetEdit(0)->text(),
                GameScens[2]->GetEdit(1)->text(),
                GameScens[2]->GetEdit(2)->text());
    }
}
void SceneManager::slotPresEscKey()
{
    emit signalPresEscKey();
}
void SceneManager::slotPresKey(QKeyEvent* event)
{
    emit signalPresKey(event);
}
